//
//  MachineSelectTableViewCell.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/4/16.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "MachineSelectTableViewCell.h"

#define STEPER_WIDTH 120

@interface MachineSelectTableViewCell()

@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *numberLabel;
@property (nonatomic,strong) HJCAjustNumButton *steper;
@property (nonatomic,copy) NSString *labelTitle;
@property (nonatomic,copy) NSString *unitStr;

@end
@implementation MachineSelectTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.contentView.width = kScreen_Width;
    //标题
    self.titleLabel = [UILabel labelSingleLineFontSize:kListDetailFontSize content:@"轻型商用空调"];
    self.titleLabel.x = CELL_PADDING_LEFT;
    self.titleLabel.y = CELL_PADDING_TOP;
    self.titleLabel.textColor = CM_TABLE_DETAIL_COLOR;
    [self.contentView addSubview:self.titleLabel];
    
    //标题
    self.numberLabel = [UILabel labelSingleLineFontSize:kListDetailFontSize content:@"1台"];
    self.numberLabel.x = CELL_PADDING_LEFT;
    self.numberLabel.textColor = CM_NAVIGATION_RED_COLOR;
    self.numberLabel.y = CGRectGetMaxY(self.titleLabel.frame) + CELL_CONTENT_MARGIN;
    [self.contentView addSubview:self.numberLabel];
    
    //设置cell的高度
    self.contentView.height = CGRectGetMaxY(self.numberLabel.frame) + CELL_PADDING_BOTTOM;
    
//    self.steper = [[UIStepper alloc]initWithFrame:CGRectMake(kScreen_Width - CELL_PADDING_RIGHT - STEPER_WIDTH, CELL_PADDING_TOP, 0, 0)];
//    self.steper.maximumValue = 100;
//    self.steper.minimumValue = 0;
//    self.steper.stepValue = 1;//每次增减的值
//    self.steper.tintColor = CM_TABLE_DETAIL_COLOR;
//    [self.steper addTarget:self action:@selector(stepperValueIschanged) forControlEvents:UIControlEventValueChanged];
//    [self.contentView addSubview:self.steper];
//    self.steper.centerY = self.contentView.height/2;
//    self.steper.x = kScreen_Width - CELL_PADDING_RIGHT - self.steper.width;
    // 创建对象
    self.steper = [[HJCAjustNumButton alloc] init];
    // 设置Frame，如不设置则默认为(0, 0, 110, 30)
//    self.steper.frame = CGRectMake(kScreen_Width - CELL_PADDING_RIGHT - STEPER_WIDTH, CELL_PADDING_TOP, 0, 0);
    // 内容更改的block回调
    @weakObj(self)
    self.steper.callBack = ^(NSString *currentNum){
        int value = [currentNum intValue];
        NSString *title = [NSString stringWithFormat:@"%d台", value];
        if (Weakself.unitStr != nil) {
            title = [NSString stringWithFormat:@"%d%@", value, Weakself.unitStr];
        }
        Weakself.numberLabel.text = title;
        Weakself.numberLabel.size = [Tools sizeOfSingleLineText:Weakself.numberLabel.text withSize:kListDetailFontSize];
        
        if (Weakself.SteperValueChangedBlock) {
            Weakself.SteperValueChangedBlock(value);
        }
    };
    // 加到父控件上
    [self.contentView addSubview:self.steper];
    self.steper.centerY = self.contentView.height/2;
    self.steper.x = kScreen_Width - CELL_PADDING_RIGHT - self.steper.width;
}

//- (void)stepperValueIschanged {
//    int value = (int)self.steper.value;
//    NSString *title = [NSString stringWithFormat:@"%d台", value];
//    if (self.unitStr != nil) {
//        title = [NSString stringWithFormat:@"%d%@", value, self.unitStr];
//    }
//
//    self.numberLabel.text = title;
//    self.numberLabel.size = [Tools sizeOfSingleLineText:self.numberLabel.text withSize:kListDetailFontSize];
//
//    if (self.SteperValueChangedBlock) {
//        self.SteperValueChangedBlock(value);
//    }
//}

- (void)setTitle:(NSString*)title {
    self.labelTitle = title;
    CGSize size = [Tools sizeOfSingleLineText:title withSize:kListDetailFontSize];
    self.titleLabel.size = size;
    self.titleLabel.text = title;
}

- (void)setNumber:(int)number andUnit:(NSString *)unit {
    self.steper.currentNum = [NSString stringWithFormat:@"%d", number];
    self.unitStr = unit;
    self.numberLabel.text = [NSString stringWithFormat:@"%d%@", number, self.unitStr];
    self.numberLabel.size = [Tools sizeOfSingleLineText:self.numberLabel.text withSize:kListDetailFontSize];
}

- (CGFloat)getHeigh{
    return self.contentView.height;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
